/**
 * Created by Amaris on 2016/12/6.
 */


var util = require('../utils/util.js');
var daoUtil = require('../utils/daoUtil');
var uuid = require('node-uuid');

var sellerClass = require('../class/seller.js');

module.exports = {

    create: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var seller = new sellerClass();

        var checkResult = seller.checkParams(
            [
                'name',
                'createUid',
                'enable'
            ],
            params);
        if (checkResult) {
            cb( util.getErrorRes('400', checkResult), null);
            return;
        } else {
            seller.init(params);
        }

        models.seller.findOrCreate(seller.getParams(), function (err, data) {
            if (err || !data) {
                //var err = new Error();
                //err.status = 500;
                //err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    },

    getInfo: function (params, cb) {
        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var seller = new sellerClass();

        var checkResult = seller.checkParams(['rowId'], params);
        if (checkResult) {
            cb( util.getErrorRes('400', checkResult),null);
            return;
        } else {
            seller.init(params);
        }

        models.seller.findOne(seller.getParams(), function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));

        });
    },
    //获取卖方列表
    getSellerList: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        models.seller.find().exec(function (err, data){
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });

    },
    //按名称查询卖方列表
    getlistByName: function (params, cb) {
        var that = this;
        var validate = daoUtil.validate(['name'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            models.seller.find({where:{name:{ 'like': '%'+params.name+'%' },enable:'1'},sort:'createdAt asc'}).exec(function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },

    //获取卖方列表
    getSellerContactList: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var seller = new sellerClass();

        var checkResult = seller.checkParams(['rowId'], params);
        uuid.v1();
        if (checkResult) {
            models.seller.find().populate("contacts",{or:[{name:{ 'like': '%'+params.queryStr+'%' }},{industry:{ 'like': '%'+params.queryStr+'%' }}]}).exec(function (err, data){
                if (err || !data) {
                    var err = new Error();
                    err.status = 500;
                    err.message = '系统错误';
                    cb(err, null);
                    return;
                }
                cb(null, util.getSuccessRes(data));
            });

            return;
        } else {
            seller.init(params);
            models.seller.find(seller.getParams()).populate("contacts",{or:[{name:{ 'like': '%'+params.queryStr+'%' }},{industry:{ 'like': '%'+params.queryStr+'%' }}]}).exec(function (err, data){
                if (err || !data) {
                    var err = new Error();
                    err.status = 500;
                    err.message = '系统错误';
                    cb(err, null);
                    return;
                }
                cb(null, util.getSuccessRes(data));
            });

        }


    },
    //todo:改为软删除

    //删除卖方
    delSeller: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        var rowId=params.rowId;
        models.seller.destroy({'rowId':rowId}).exec(function (err, data){
            if (err || !data) {
                cb(err, null);
                return;
            }
            if(data&&data.length==0){
                var err = new Error();
                err.status = 500;
                err.message = '删除失败';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });

    },

    //获取有效卖方列表
    getEnabledSellerList: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        var seller = new sellerClass();

        var checkResult = seller.checkParams(['rowId'], params);

        if (checkResult) {
            params={enable:'1'};
        }else{
            params.enable='1';
        }
        seller.init(params);
        models.seller.find(seller.getParams()).exec(function (err, data){
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });

    },
    //软删除卖方
     softDelSeller: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        var rowId=params.rowId;



        models.seller.update({'rowId':rowId},{'enable':'-1'}).exec(function (err, data){
            if (err || !data) {
                cb(err, null);
                return;
            }
            if(data&&data.length==0){
                var err = new Error();
                err.status = 500;
                err.message = '删除失败';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });

    },

};



